package com.zyh.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.PageInfo;
import com.zyh.entity.Jar;
import com.zyh.service.JarService;
import com.zyh.service.impl.JarServiceImpl;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/jar")
public class JarController {
    JarService jarService = new JarServiceImpl();

    @RequestMapping("/deleteJarsMvc")
    public String deleteJars(int[] ids){
        String filePathAndName="d:/JarMan/";
        System.out.println(ids.toString());
        List<Jar> jarList = jarService.selectJars(ids);
        try {
            for (Jar jar : jarList
                 ) {
                String filePath = filePathAndName+jar.getFileName();
                filePath = filePath.toString();
                File myDelFile = new File(filePath);
                myDelFile.delete();
            }
        } catch (Exception e) {
            System.out.println("删除文件操作出错");
            e.printStackTrace();
        }

        jarService.deleteJars(ids);

        return  "redirect:/jar/selectAllJarByPageMvc.do";
    }

    @RequestMapping("/selectOneMvc")
    public String selectOne(){

        return "";
    }

    @RequestMapping("/selectAllJarByPageMvc")
    public String selectAllJarByPageMvc(String curPageStr, Model model ,String nameSearch){
        int curPage = 1;
        if(curPageStr!=null){
            curPage = Integer.parseInt(curPageStr);
        }
        int pageSize = 10;
        PageInfo<Jar> jarPageInfo = jarService.selectAllJarByPage(curPage, pageSize , nameSearch);
        model.addAttribute("jarPageInfo",jarPageInfo);
        return "/selectByPage.jsp";
    }
    /*
    * 上传文件
    * */
    @RequestMapping("/uploadJarMvc")
    public String upload(MultipartFile uploadFile,Jar jar){
//        生成新的文件名
        String newFileName = (UUID.randomUUID().toString())+uploadFile.getOriginalFilename();
        //指定生成路径
        File file = new File("d:/JarMan/"+newFileName);
//        为对象赋值
        jar.setName(uploadFile.getOriginalFilename());
        jar.setSize(uploadFile.getSize());
        jar.setFileName(newFileName);
        try {
            jarService.insertOneJar(jar);
//            调用方法将文件上传到指定路径
            uploadFile.transferTo(file);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/jar/selectAllJarByPageMvc.do";
    }
    /*
    * 下载文件
    * */
    @RequestMapping("/downloadJarMvc")
    public void download(String nameOld,String fileUUIDName,HttpServletResponse response) throws IOException{
        //1.通过文件名转换为文件 通过UUID找到对应文件
        File sourceFile=new File("d:/JarMan/"+fileUUIDName);
        //2.设置响应格式和文件的名字
        response.setHeader("content-disposition","attachment;fileName="+nameOld);
        //3.把文件放入到response里面。
        FileUtils.copyFile(sourceFile,response.getOutputStream());

    }
    /*
    * 从数据库批量导出到本地Excel表格中
    * */
    @RequestMapping("/outDownMvc")
    public void outDown(HttpServletResponse response) throws IOException{
        /**
         * 准备数据
         */
        List<Jar> jarList = jarService.selectAll();
        /**
         * 导出参数对象
         * 参数1 标题
         * 参数2 表名
         */
        ExportParams params = new ExportParams("所有的jar数据","jars");
        Workbook workbook = ExcelExportUtil.exportExcel(params, Jar.class, jarList);
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("导出的Jar包.xls","utf-8"));
//        workbook.write(new FileOutputStream("d:/easyBrands.xls"));
        workbook.write(response.getOutputStream());
    }
    /*
    * 从Excel表格中批量导入到数据库中
    * */
    @RequestMapping("/inLoadMvc")
    public String inLoad(MultipartFile fileInLoad) throws Exception {
        //将上传的文件放入输入流中
        InputStream inputStream = fileInLoad.getInputStream();
        /**
         * 导入参数对象
         * setTitleRows 声明标题占有的行数
         * setHeadRows 声明表头占有的行数
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        /**
         * 导入方法
         * 参数1 流
         * 参数2 实体类对象
         * 参数3 导入参数对象
         */
        List<Jar> jarList = ExcelImportUtil.importExcel(inputStream, Jar.class, importParams);
//        循环执行插入sql
        for (Jar jar : jarList
             ) {
            jarService.insertOneJar(jar);
        }
        return "/jar/selectAllJarByPageMvc.do";
    }

    @RequestMapping("/selectBySearchMvc")
    public String selectBySearchMvc(String name){

        return "/";
    }
}
